package com.yiwei.jmx.activity;

import android.os.Bundle;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.jmx.R;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.api.bean.DealApplyDetailBean;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.Spans;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.util.Util;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class RepayStatusActivity extends BaseActivity {

    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.img_status_one)
    ImageView imgStatusOne;
    @BindView(R.id.txt_status_one_top)
    TextView txtStatusOneTop;
    @BindView(R.id.txt_status_one_bottom)
    TextView txtStatusOneBottom;
    @BindView(R.id.view_one)
    View viewOne;
    @BindView(R.id.img_status_two)
    ImageView imgStatusTwo;
    @BindView(R.id.txt_status_two_top)
    TextView txtStatusTwoTop;
    @BindView(R.id.txt_status_two_bottom)
    TextView txtStatusTwoBottom;
    @BindView(R.id.view_two_top)
    View viewTwoTop;
    @BindView(R.id.view_two_bottom)
    View viewTwoBottom;
    @BindView(R.id.img_status_three)
    ImageView imgStatusThree;
    @BindView(R.id.txt_status_three_top)
    TextView txtStatusThreeTop;
    @BindView(R.id.txt_status_three_bottom)
    TextView txtStatusThreeBottom;
    private Unbinder unbinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_repay_status);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        String deal_id = getIntent().getStringExtra("deal_id");
        String needMoney = getIntent().getStringExtra("need_money");

        if (TextUtils.isEmpty(needMoney)) {
            getRepayData(deal_id);
        } else {
            txtStatusOneBottom.setText("划扣金额" + needMoney + "元");
            txtStatusTwoBottom.setText("还款金额" + needMoney + "元");
        }

        int x24 = getResources().getDimensionPixelSize(R.dimen.x24);
        Spanned txt_tips = Spans.with(this).font("您的还款订单将在短时间内更新状态，请稍后查看\n若届时仍未更新，请", x24, 0xffB6BCDB).
                font("联系客服", x24, 0xff4C81FF).
                font("咨询", x24, 0xffB6BCDB).
                build();
        txtStatusThreeBottom.setText(txt_tips);
    }

    public void getRepayData(String deal_id) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        loadingDialog.show();
        DealApplyDetailBean bean = new DealApplyDetailBean();
        bean.deal_id = deal_id;
        Api.api_service.getDealApplyDetail(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
//                    if (TextUtils.isEmpty(result.data.true_money)) {
//                        txtStatusOneBottom.setText("划扣金额" + result.data.month_need_all_repay_money + "元");
//                        txtStatusTwoBottom.setText("还款金额" + result.data.month_need_all_repay_money + "元");
//                    } else {
                    if (result.data != null) {
                        txtStatusOneBottom.setText("划扣金额" + result.data.true_money + "元");
                        txtStatusTwoBottom.setText("还款金额" + result.data.true_money + "元");
                    }

//                    }
                });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }


    @OnClick({R.id.btn_back, R.id.txt_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.txt_right:
                Util.startActivity(this, MyBorrowRecordActivity.class);
                break;
        }
    }
}
